﻿using System;
using System.Collections.Generic;
using System.Text;


namespace PIM_ATE
{
    class JcPIMRunner
    {
        public event EventHandler evt_show_error_handle;
        protected virtual void On_evt_show_error_handle(object obj, EventArgs e)
        {
            if (this.evt_show_error_handle != null)
                this.evt_show_error_handle(obj, e);
        }

        public event EventHandler evt_show_log_handle;
        protected virtual void On_evt_show_log_handle(object obj, EventArgs e)
        {
            if (this.evt_show_log_handle != null)
                this.evt_show_log_handle(obj, e);
        }

        public event EventHandler evt_get_point_handle;
        protected virtual void On_evt_get_point_handle(object obj, EventArgs e)
        {
            if (this.evt_get_point_handle != null)
                this.evt_get_point_handle(obj, e);
        }

        public event EventHandler evt_complete_test_handle;
        protected virtual void On_evt_complete_test_handle(object obj, EventArgs e)
        {
            if (this.evt_complete_test_handle != null)
                this.evt_complete_test_handle(obj, e);
        }

        public virtual void start_settings(JcPIMDataSettings settings)
        {
        }

        public virtual void start_test()
        {
        }

        public virtual void stop_test()
        {
        }

        public virtual List<JcPIMDataResults> get_results()
        {
            return null;
        }

        public class XPlot
        {
            public XPlot(double x1, double x2, double rx)
            {
                X1 = x1;
                X2 = x2;
                RX = rx;
            }
            public XPlot(double x1, double x2, double rx, double val)
            {
                X1 = x1;
                X2 = x2;
                RX = rx;
                VAL = val;
            }
            public double X1 { get; private set; }
            public double X2 { get; private set; }
            public double RX { get; private set; }
            public double VAL { get; private set; }
        }
        

        protected double sumPIMFreq(double f1, double f2, int order, bool ishigh)
        {
            if (!ishigh)
            {
                int coe1 = order / 2 + 1;
                int coe2 = order / 2;
                double value = coe1 * f1 - coe2 * f2;
                int i = (int)(value * 100);
                value = i / 100.0;
                return value;
            }
            else
            {
                int coe2 = order / 2 + 1;
                int coe1 = order / 2;
                double value = coe2 * f2 - coe1 * f1;
                int i = (int)(value * 100);
                value = i / 100.0;
                return value;
            }

        }

        protected double sumF1FromPIMFreq(double f2, double fpim, int order, bool ishigh)
        {
            if (!ishigh)
            {
                int coe1 = order / 2 + 1;
                int coe2 = order / 2;
                double value = (coe2 * f2 + fpim) / coe1;
                int i = (int)(value * 100);
                value = i / 100.0;
                //value = Math.Round(value, 2);
                return value;
            }
            else
            {
                int coe2 = order / 2 + 1;
                int coe1 = order / 2;
                double value = (coe2 * f2 - fpim) / coe1;
                int i = (int)(value * 100);
                value = i / 100.0;
                return value;
            }        
    
      }

        protected double sumF2FromPIMFreq(double f1, double fpim, int order, bool ishigh)
        {
            if (!ishigh)
            {
                int coe1 = order / 2 + 1;
                int coe2 = order / 2;
                double value = (coe1 * f1 - fpim) / coe2;
                int i = (int)(value * 100);
                value = i / 100.0;
                //value = Math.Round(value, 2);
                return value;
            }
            else
            {
                int coe2 = order / 2 + 1;
                int coe1 = order / 2;
                double value = (coe1 * f1 + fpim) / coe2;
                int i = (int)(value * 100);
                value = i / 100.0;
                //value = Math.Round(value, 2);
                return value;
            }
        }

        protected List<XPlot> getXPointByTxStep(double F1s, double F1e, 
                                                double F2s, double F2e,
                                                double Step1, double Step2, int Order, bool ishigh)
        {
            List<XPlot> lstPoint = new List<XPlot>();
            double n1 = 0, n2 = 0, f = 0;
            if (Step1 <= 0) Step1 = 1;
            n1 = Math.Ceiling((F1e - F1s) / Step1) + 1;
            if (Step2 <= 0) Step2 = 1;
            n2 = Math.Ceiling((F2e - F2s) / Step2) + 1;

            f = F1s;
            for (int i = 0; i < n1; ++i)
            {//上扫频
                if (f > F1e) f = F1e;
                double fpim = sumPIMFreq(f, F2e, Order, ishigh);
                lstPoint.Add(new XPlot(f, F2e, fpim));
                //lstPoint.Add(new XPlot(f, F2e));
                f += Step1;
            }

            f = F2e;
            for (int i = 0; i < n2; ++i)
            {//下扫频
                if (f < F2s) f = F2s;
                double fpim = sumPIMFreq(F1s, f, Order, ishigh);
                lstPoint.Add(new XPlot(F1s, f, fpim));
                f -= Step2;
            }
            return lstPoint;
        }

        protected List<XPlot> getXPointByRxStep(double F1Set, double F2Set,
                                                double RXs, double RXe,
                                                double StepRx, int Order, bool ishigh)
        {
            List<XPlot> lstPoint = new List<XPlot>();
            double n1 = 0, n2 = 0, f = 0;
            double n = Math.Ceiling((RXe - RXs) / StepRx) + 1;
            f = RXs;
            for (int i = 0; i < n; ++i)
            {
                if (f > RXe) f = RXe;

                double f1 = sumF1FromPIMFreq(F2Set, f, Order, ishigh);
                if (f1 >= F1Set && f1 <= F2Set)
                {
                    //continue;
                    lstPoint.Add(new XPlot(f1, F2Set, f));
                    n1++;
                }

                f1 = sumF2FromPIMFreq(F2Set, f, Order, ishigh);
                if (f1 >= F1Set && f1 <= F2Set)
                {
                    //continue;
                    lstPoint.Add(new XPlot(f1, F2Set, f));
                    n1++;
                }
            }

            f = RXs;
            for (int i = 0; i < n; ++i)
            {
                if (f > RXe) f = RXe;
                double f2 = sumF2FromPIMFreq(F1Set, f, Order,ishigh);
                if (f2 >= F1Set && f2 <= F2Set)
                {
                    //continue;
                    lstPoint.Add(new XPlot(F1Set, f2, f));
                    n2++;
                }

                f2 = sumF1FromPIMFreq(F1Set, f, Order, ishigh);
                if (f2 >= F1Set && f2 <= F2Set)
                {
                    //continue;
                    lstPoint.Add(new XPlot(F1Set, f2, f));
                    n2++;
                }
            }
            return lstPoint;
        }
    }
}
